package com.xhwl.centiotdata.handler.alarm.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xhwl.centiotdata.handler.alarm.AlarmFactory;
import com.xhwl.centiotdata.handler.alarm.AlarmHandler;
import com.xhwl.centiotdata.utils.RedisUtils;
import com.xhwl.common.constant.cent.alarm.AlarmConstant;
import com.xhwl.common.dto.cent.alarm.haikang.HaiKangEventsBody;
import com.xhwl.common.dto.cent.alarm.haikang.VariableJson;
import com.xhwl.common.dto.cent.alarm.iot.IotAlarmBody;
import com.xhwl.common.dto.cent.device.DeviceDTO;
import com.xhwl.common.exception.DataException;
import com.xhwl.common.pojo.cent.alarm.AlarmManagement;
import com.xhwl.common.query.cent.device.DevicePageQuery;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.time.ZoneId;
import java.util.*;

/**
 * @Author Leon
 * @Description: TODO
 * @Date: 2022/7/8 15:11
 */
@Component
@Slf4j
public class AlarmHuaWeiIotDAIntrusionAlarmHandler extends AlarmHandler {

    @Override
    public void afterPropertiesSet() throws Exception {
        AlarmFactory.register("IntrusionAlarm", this);
    }

    @Override
    public void pushHuaWeiIotDAIotAlarmBody(IotAlarmBody t, DeviceDTO device) {
        HaiKangEventsBody haiKangJson = parseObject(JSONObject.parseObject(t.getProperties().toString()).get("InvadeAlarmEvent").toString(), HaiKangEventsBody.class);

        Map<String, String> entries = redisTemplate.opsForHash().entries(RedisUtils.format(40));

        if (StringUtils.isNotBlank(Optional.ofNullable(haiKangJson.getEventDetails())
                .map(Object::toString).orElse(""))) {
            List<HaiKangEventsBody> inner = JSONObject.parseArray(JSONObject.toJSONString(haiKangJson.getEventDetails()), HaiKangEventsBody.class);
            inner.forEach(echo -> {
                if (StringUtils.isNotBlank(entries.get(echo.getEventType()))) {
                    AlarmManagement alarmManagement = new AlarmManagement();
                    alarmManagement.setAlarmTime(Optional.ofNullable(haiKangJson.getHappenTime())
                            .map(unit -> Date.from(unit.atZone(ZoneId.systemDefault()).toInstant())).orElse(new Date()));
                    alarmManagement.setDeviceId(device.getId());
                    alarmManagement.setAlarmInventoryCode(entries.get(echo.getEventType()));
                    sendMq(AlarmConstant.JINMAO_TOPIC, alarmManagement);
                }
            });
        } else {
            if (StringUtils.isNotBlank(entries.get(haiKangJson.getEventType()))) {
                AlarmManagement alarmManagement = new AlarmManagement();
                alarmManagement.setAlarmTime(Optional.ofNullable(haiKangJson.getHappenTime())
                        .map(unit -> Date.from(unit.atZone(ZoneId.systemDefault()).toInstant())).orElse(new Date()));
                alarmManagement.setDeviceId(device.getId());
                if (Objects.nonNull(haiKangJson.getData())) {
                    JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(haiKangJson.getData()));
                    String imageUrl = Optional.ofNullable(jsonObject).map(unit -> unit.getString("eventDescription")).map(unit ->
                            JSONObject.parseArray(jsonObject.getString(unit), VariableJson.class)
                    ).map(unit -> unit.get(0)).map(VariableJson::getImageUrl).orElse("");
                    alarmManagement.setImgUrl(imageUrl);
                }
                alarmManagement.setAlarmInventoryCode(entries.get(haiKangJson.getEventType()));
                sendMq(AlarmConstant.JINMAO_TOPIC, alarmManagement);
            }
        }
    }
}
